// pages/order-status/order-status.js
const db = wx.cloud.database();
const util = require('../../utils/util.js');

Page({
  data: {
    orderDetail: null,
    loading: true
  },

  onLoad: function (options) {
    if (options.orderId) {
      this.getOrderDetail(options.orderId);
    }
  },

  getOrderDetail: function(orderId) {
    db.collection('orders').doc(orderId).get().then(res => {
      res.data.createTimeFormatted = util.formatTime(new Date(res.data.createTime));
      this.setData({
        orderDetail: res.data,
        loading: false
      });
    }).catch(err => {
      this.setData({
        loading: false
      });
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
      console.error('获取订单详情失败', err);
    });
  },

  goHome: function() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  }
})